<?php

class user extends BACKEND_Controller {
    /*
      Project     : 48c6c450f1a4a0cc53d9585dc0fee742
      Created on  : Mar 16, 2013, 11:29:15 PM
      Author      : Truong Khuong - khuongxuantruong@gmail.com
      Description :
      Purpose of the stylesheet follows.
     */

    function __construct() {
        parent::__construct();
        $this->load->model('backend/user_model');
        $this->smarty = new CI_Smarty3();
        $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
    }

    function index() {
        $assigns['users'] = $this->user_model->gets();
        $this->smarty
                ->assign($assigns)
                ->display("backend/02_user/list");
    }

    function editor($id = "") {
        $assigns['user'] = null;
        if ($id !== "")
            $assigns['user'] = $this->user_model->get($id);
        $this->smarty
                ->assign($assigns)
                ->display("backend/02_user/editor");
    }
    
    function randomstring(){
        echo random_string('alnum', rand(4, 8)*2);
    }
    
    function save() {
        $R["result"] = -1;
        $R["message"] = ascii_to_entities("Access denied. Bad request.");

        $params["ause_name"] = $this->input->post("ause_name");
        $params["ause_username"] = $this->input->post("ause_username");
        $params["ause_position"] = (int) $this->input->post("ause_position");
        $params["ause_status"] = $this->input->post("ause_status");
        $params["ause_email"] = $this->input->post("ause_email");
        $apri_id = $this->input->post("ause_id");
        if ($apri_id != false) {
            $user = $this->user_model->get($apri_id);
            if($user==null){
                $R["result"] = -904;
                $R["message"] = ascii_to_entities("User not exist."); 
                goto result;
            }
            if($this->input->post("ause_password")!=false){
                $params["ause_password"]=md5($user->ause_username.$this->input->post("ause_password").$user->ause_secretkey);
            }
            $params["ause_update"] = date('Y-m-d H:i:s');
            $rok = @$this->user_model->update($apri_id, $params);
        } else {
            $params["ause_key"] = random_string('alnum', 8);
            $params["ause_salt"] = random_string('alnum', 8);
            $params["ause_secretkey"] = random_string('alnum', 32);
            $params["ause_password"]=md5($params["ause_username"].$this->input->post("ause_password").$params["ause_secretkey"]);
            $rok = @$this->user_model->insert($params);
        }
        if ($rok === true) {
            $R["result"] = 1;
            $R["message"] = ascii_to_entities("Dữ liệu đã được hệ thống ghi nhận.");
        } else {
            $R["result"] = -1;
            $R["error_number"] = $this->db->_error_number();
            $R["error_message"] = $this->db->_error_message();
            $R["message"] = ascii_to_entities("
                    Dữ liệu chưa được hệ thống ghi nhận.<br/>
                    Vui lòng kiểm tra dữ liệu và thử lại.<br/>
                    ");
            if ($R["error_number"] != 0)
                $R["message"].=ascii_to_entities("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
        }
        result:
        echo json_encode($R);
    }

}

?>